package io.geekidea.springboot.handler;

import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(JsonNode.class)
public class CustomJacksonTypeHandler extends JacksonTypeHandler {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public CustomJacksonTypeHandler() {
        super(JsonNode.class);
    }

    public CustomJacksonTypeHandler(Class<?> type) {
        super(type);
    }

    @Override
    public JsonNode parse(String json) {
        try {
            return objectMapper.readTree(json);
        } catch (Exception e) {
            throw new RuntimeException("Failed to parse JSON", e);
        }
    }

    @Override
    public String toJson(Object obj) {
        if (obj instanceof JsonNode) {
            return obj.toString();
        }
        throw new IllegalArgumentException("Expected JsonNode but got " + obj.getClass());
    }
} 